﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class pingpang : MonoBehaviour {

	public float sleepTimeinA = 1.0f;
	public float sleepTimeInB =0.2f;
	public float moveSpeedToB=100.0f;
	public float moveSpeedToA=50.0f;
	// Use this for initialization
	Vector3 apoint;
	Vector3 bpoint;
	void Start () {
		apoint = this.transform.localPosition;
		var pp = this.GetComponentInChildren<point>().transform;
		bpoint = this.transform.parent.worldToLocalMatrix.MultiplyPoint( pp.position);
		pp.gameObject.SetActive (false);
		firstcall++;
	}

	int state =0;
	float timer=0;
	// Update is called once per frame
	int firstcall=0;
	void Update () {
		timer += Time.deltaTime;
		if (state == 0) {
			if (timer > sleepTimeinA) {
				state = 1;
				timer = 0;
				return;
			}
		}
		if (state == 1) {
			var dir = bpoint - this.transform.localPosition;
			var len = dir.magnitude;
			if (len < 0.001) {
				state = 2;
				timer = 0;
				return;
			}
			var add = moveSpeedToB * Time.deltaTime;
			if (add > len)
				add = len;
			this.transform.localPosition = this.transform.localPosition + add * dir.normalized;

		}
		if (state == 2) {
			if (timer > sleepTimeInB) {
				state = 3;
				timer = 0;
				return;
			}
		}
		if (state == 3) {
			var dir = apoint - this.transform.localPosition;
			var len = dir.magnitude;
			if (len < 0.001) {
				state = 0;
				timer = 0;
				return;
			}
			var add = moveSpeedToA * Time.deltaTime;
			if (add > len)
				add = len;
			this.transform.localPosition = this.transform.localPosition + add * dir.normalized;
		}
	}
}
